using System.Data.SqlClient;
using NUnit.Framework;

namespace DatabaseUnitTesting.Tests
{
    [TestFixture]
    public class StoredProcedureUnitTesterTests
    {
        private SqlConnection connection;
        private string database = "astyler";
        private string server = "BNEE-X64";

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;
            connection = new SqlConnection(connectionString);
            connection.Open();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            connection.Close();
        }

        [Test]
        public void CompareBoundariesTest()
        {
            string filename = "C:/Temp/BoundariesTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TwoAsc");
            sput.SetInputParameter("@count", "0");
            sput.SetInputParameter("@rows", "100");
            sput.OutputToFile(filename);
            Assert.IsTrue(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "1");
            sput.OutputToFile(filename);
            Assert.IsTrue(sput.CompareToFile(filename));

            sput.SetInputParameter("@rows", "0");
            sput.OutputToFile(filename);
            Assert.IsTrue(sput.CompareToFile(filename));

            sput.SetInputParameter("@rows", "1");
            sput.OutputToFile(filename);
            Assert.IsTrue(sput.CompareToFile(filename));
        }

        [Test]
        public void CompareRandomOrderedResultsToFileTest()
        {
            string filename = "C:/Temp/RandomOrderTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TwoRand");
            sput.SetInputParameter("@count", "2");
            sput.OutputToFile(filename);
            Assert.IsTrue(sput.CompareToFile(filename));
        }

        [Test]
        public void VariousFailingComparisonsTest()
        {
            string filename = "C:/Temp/FailingTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TwoAsc");
            sput.SetInputParameter("@rows", "100");
            sput.SetInputParameter("@count", "1");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "2");
            Assert.IsFalse(sput.CompareToFile(filename));
            sput.SetInputParameter("@count", "0");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "0");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "1");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "49");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "50");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "1");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "0");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "0");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "1");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "50");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "4");
            sput.SetInputParameter("@rows", "49");
            Assert.IsFalse(sput.CompareToFile(filename));

            sput.SetInputParameter("@count", "10");
            sput.SetInputParameter("@rows", "50");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "50");
            sput.SetInputParameter("@rows", "10");
            Assert.IsFalse(sput.CompareToFile(filename));
        }

        [Test]
        public void MultipleTypesTest()
        {
            string filename = "C:/Temp/TypesTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TypedRand");
            sput.SetInputParameter("@count", "3");
            sput.OutputToFile(filename);
            sput.OutputOnFailureTarget("C:/Temp/procTest.sql", "C:/Temp/fileTest.sql", null, null, "dbo",
                                     "failureTestTable");
            sput.OutputOnFailure = true;
            sput.SetInputParameter("@count", "3");
            Assert.IsTrue(sput.CompareToFile(filename));
        }

        [Test]
        public void FailureOutputTest()
        {
            string filename = "C:/Temp/FailureOutputTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TypedRand");
            sput.OutputToFile(filename);
            sput.SetInputParameter("@count", "2");
            sput.OutputOnFailureTarget("C:/Temp/procTest.sql", "C:/Temp/fileTest.sql", "comparer1", "comparer2", "dbo",
                                      "failureTestTable");
            sput.OutputOnFailure = true;
            Assert.IsFalse(sput.CompareToFile(filename));
        }

        [Test]
        public void UnorderedResultSetsTest()
        {
            string filename1 = "C:/Temp/UnorderedResultSetsTest.txt";
            StoredProcedureUnitTester sput = new StoredProcedureUnitTester(connection, database, "TwoSets");
            sput.SetInputParameter("@rows1", "1");
            sput.SetInputParameter("@rows2", "2");
            sput.OutputToFile(filename1);
            sput.SetInputParameter("@rows1", "2");
            sput.SetInputParameter("@rows2", "1");
            Assert.IsTrue(sput.CompareToFile(filename1));
        }

    }
}